home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / bbs / pub_acc.taz / pub_acc / makematch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-18  |  4.8 KB  |  213 lines

  1. #include <stdio.h>
  2. #include <pwd.h>
  3. #define FILENAME "/home/games/match.list"
  4.  
  5. struct qs {
  6.     char *q;
  7.     char *a;
  8.     };
  9.  
  10. struct qs q_sex = {"Are you Male or Female", "MF"};
  11. struct qs q_gay = {"Are you Straight, Gay or Bisexual", "SGB"};
  12. struct qs q_age = {"How old are you", NULL};
  13. struct qs qlist[] = {
  14.     {"Do you consider yourself atheletic", "YN"},
  15.     {"How attractive do you consider yourself on a scale from 1 to 5 (best)"
  16.         ,"12345"},
  17.     {"Are you a vegetarian", "YN"},
  18.     {"Are you an Introvert or an Extrovert", "IE"},
  19.     {"Do you prefer Rock, Classical, or Jazz music", "RCJ"},
  20.     {"Do you enjoy dancing", "YN"},
  21.     {"Do you prefer Cats or Dogs as pets", "CD"},
  22.     {"Do you enjoy movies", "YN"},
  23.     {"Do you enjoy spectator sports", "YN"},
  24.     {"Are you on a diet", "YN"},
  25.     {"Do you like the outdoors", "YN"},
  26.     {"Do you remember your dreams", "YN"},
  27.     {"Are you politically Liberal or Conservative", "LC"},
  28.     {"Have you ever shoplifted", "YN"},
  29.     {"Are you usually Late or Early for appointments", "LE"},
  30.     {"Would you like a long term relationship","YN"},
  31.     {"Do you like to be alone", "YN"},
  32.     {"Are you Tall or Short", "TS"},
  33.     {"Do you enjoy travelling", "YN"},
  34.     {"Do you smoke", "YN"},
  35.     {"Were your parents separated or divorced", "YN"},
  36.     {"Are you prefer Large, Midsized or Small cars", "LMS"},
  37.     {"Do you write poetry", "YN"},
  38.     {"Do you enjoy spending money", "YN"},
  39.     {"Are you strongly religious", "YN"},
  40.     {"Do you get along well with most people", "YN"},
  41.     {"Are you unusually nervous around strangers", "YN"},
  42.     {"Do you like the dark","YN"},
  43.     {"Do you usually sleep on your Back, Left-side, Right-side or Stomach",
  44.         "BLRS"},
  45.     {"Do you often contribute to charity", "YN"},
  46.     {"Do you consider yourself an intellectual", "YN"},
  47.     {"Do you like opera", "YN"},
  48.     {"Which do you prefer: Rain, Snow, Thunder or Windstorms", "RSTW"},
  49.     {"Do you snore", "YN"},
  50.     {"Do you like dressing up", "YN"},
  51.     {"Do you spell well", "YN"},
  52.     {"Do you have more than two credit cards", "YN"},
  53.     {"Do you often work overtime", "YN"},
  54.     {"Do you have lots of friends", "YN"},
  55.     {"Do you have a poor self image", "YN"},
  56.     {"Are you more interested in Art, Business, sCience or sPorts ",
  57.         "ABCP"},
  58.     {"Can you touch your toes", "YN"},
  59.     {"Do you enjoy shopping", "YN"},
  60.     {"Do you wear glasses or contact lenses", "YN"},
  61.     {"Would you rather shop for Cloths, Records, Books or Food", "CRBF"},
  62.     {"Do you enjoy winter sports", "YN"},
  63.     {"Can you swim", "YN"},
  64.     {"Do you believe opposites attract", "YN"},
  65.     {NULL,NULL}};
  66.  
  67. struct ff {
  68.     char name[9];
  69.     char sex;
  70.     char sexmask;
  71.     int age;
  72.     int score;
  73.     };
  74.  
  75. main()
  76. {
  77. struct ff me;
  78. int gay, i;
  79. struct qs *q;
  80. FILE *fp;
  81. int fd;
  82. char myname[9];
  83. struct passwd *pent, *getpwuid();
  84.  
  85.     pent = getpwuid(getuid());
  86.     strncpy(myname,pent->pw_name,8);
  87.     if ((fp=fopen(FILENAME,"r")) == NULL)
  88.     {
  89.         printf("cannot open file\n");
  90.         exit(1);
  91.     }
  92.  
  93.     while (fread(&me,sizeof(me),1,fp) == 1)
  94.     {
  95.         if (strcmp(me.name,myname)==0)
  96.         {
  97.             findmatch(fp,&me);
  98.             exit(0);
  99.         }
  100.     }
  101.  
  102.     strcpy(me.name,myname);
  103.     me.sex = ask(&q_sex)+1;
  104.     gay = ask(&q_gay);
  105.     if (gay == 0)
  106.         me.sexmask = 3-me.sex;
  107.     else if (gay == 1)
  108.         me.sexmask = me.sex;
  109.     else
  110.         me.sexmask = 3;
  111.  
  112.     me.age = ask(&q_age);
  113.  
  114.     me.score = 0;
  115.     for (i = 0; myname[i] != '\0'; i++)
  116.         me.score += myname[i];
  117.     for (q = qlist; q->q != NULL; q++)
  118.         me.score += ask(q);
  119.  
  120.     if ((fd = open(FILENAME,2)) < 0)
  121.     {
  122.         printf("Can't write file\n");
  123.         exit(1);
  124.     }
  125.     lseek(fd,0L,2);
  126.     write(fd,&me,sizeof(me));
  127.     close(fd);
  128.     findmatch(fp,&me);
  129. }
  130.  
  131. findmatch(fp,me)
  132. FILE *fp;
  133. struct ff *me;
  134. {
  135. struct ff him;
  136. int bscore[5];
  137. char bname[5][9];
  138. char tname[9];
  139. int score;
  140. int i,t,da;
  141.  
  142.     for (i = 0; i < 5; i++)
  143.         bscore[i] = -1;
  144.  
  145.     printf("seeking match...\n");
  146.     sleep(10);
  147.     rewind(fp);
  148.     while (fread(&him,sizeof(him),1,fp) == 1)
  149.     {
  150.         da = him.age - me->age;
  151.         if ((him.sex & me->sexmask) && (me->sex & him.sexmask) &&
  152.             da > -9 && da < 9 && strcmp(him.name,me->name))
  153.         {
  154.             score = him.score - me->score;
  155.             if (score < 0) score = -score;
  156.             score = 100 - (score % 101);
  157.             for (i = 0; i < 5; i++)
  158.             {
  159.                 if (score > bscore[i])
  160.                 {
  161.                     strcpy(tname,bname[i]);
  162.                     strcpy(bname[i],him.name);
  163.                     strcpy(him.name,tname);
  164.                     t = bscore[i];
  165.                     bscore[i] = score;
  166.                     score = t;
  167.                 }
  168.             }
  169.         }
  170.     }
  171.  
  172.     if (bscore[0] < 0)
  173.         printf("No match found.  Try later.\n");
  174.     else
  175.     {
  176.         printf("Best matches so far:\n");
  177.         for(i=0; i<5; i++)
  178.             if (bscore[i] >= 0)
  179.             printf("  %d: %s rated %d%%\n",i+1,bname[i],bscore[i]);
  180.     }
  181. }
  182.  
  183. int ask(q)
  184. struct qs *q;
  185. {
  186. int ch,cx,n;
  187. char *p, *strchr();
  188.  
  189.     if (q->a)
  190.         for (;;)
  191.         {
  192.             printf("%s [%s]? ",q->q,q->a);
  193.             if ((ch=getchar()) == '\n') continue;
  194.             while ((cx=getchar()) != '\n' && cx != EOF)
  195.                 ;
  196.             if (cx == EOF) exit(1);
  197.             if (ch >= 'a' && ch <= 'z')
  198.                 ch = ch - 'a' + 'A';
  199.             if ((p=strchr(q->a,ch)) != NULL)
  200.                 return(p - q->a);
  201.         }
  202.     else
  203.     {
  204.         printf("%s? ",q->q);
  205.         n = 0;
  206.         while ((ch=getchar()) != '\n' && ch != EOF)
  207.             if (ch >= '0' && ch <= '9')
  208.                 n = n*10 + ch - '0';
  209.         if (ch == EOF) exit(1);
  210.         return(n);
  211.     }
  212. }
  213.